﻿var dragging = false;
function render() {
    
    $(".printtag").remove();
    var selected = $("input:checked");
    var values = [];
    selected.each(function () {
        var products = $(this).val().split(",");
        var productName = products[0];
        var productGUID = products[1];
        var myObj = { "productName": productName, "productGUID": productGUID };
        values.push(myObj);
    });

    $.ajax({
        type: "POST",
        data:JSON.stringify({"productInfo":values}),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "/GetTags.ashx",
        success: function (data) {
            if (data)
            {
                $("#printTemp, #printButton").show();
                var labels = $(".printlabel");
                $.each(data, function (i) {

                    var printtag = $("<div class='printtag'></div>");
                    var qr = $("<img src='" + this.qr + "' />");
                    var tag = $("<img src='" + this.tag + "' />");
                    var productName = $("<span>" + this.productName + "</span>");
                    printtag.append(tag).append(qr).append(productName);
                    printtag.draggable({
                        snap: ".printlabel", snapMode: "inner", containment: ".printlabels", stop: function (event, ui) {
                            if (dragging) {
                                $(this).offset($(this).data("position"));
                                dragging = false;
                            }
                        }, start: function (event, ui) {

                            $(this).data("position", $(this).offset());
                            dragging = true;
                        }
                    });
                    $("#printlabels").append(printtag);
                    printtag.offset($(labels[i]).offset());
                });

                $(".printlabel").droppable({
                    accept: ".printtag", drop: function (event, ui) {
                        $(ui.draggable).offset($(this).offset());
                        dragging = false;
                    }
                });
            }
        }
    });
}
function printdiv(id) {
    $("#printlabels, .printtag, .printlabel").css("border-color", "white");
    var divElements = document.getElementById(id).innerHTML;
    //Get the HTML of whole page
    var oldPage = document.body.innerHTML;
    //Reset the page's HTML with div's HTML only
    document.body.innerHTML =
      "<html><head><title></title></head><body>" +
      divElements + "</body>";
    //window.print();
    
    
}